<template>
  <div class="box">
    <div class="title">
      <img src="" alt class="logo" />
      <!-- <span class="title-hn">ROBOT</span> -->
    </div>
    <div id="content" class="content">
      <div v-for="(item,index) in info" :key="index">
        <!-- robot -->
        <div class="info_r info_default" v-if="item.type == 'leftinfo'">
          <div class="time_r">{{item.time}}</div>
          <!-- <span class="circle circle_r">机器人头像</span> -->
          <div class="con_r con_text">
            <!-- content -->
            <div>{{item.content}}</div>
            <!-- question -->
            <div v-for="(item2,index) in item.question" :key="index">
              <div class="con_que" @click="sentMsgByVal(item2.id)">
                <div class="czkj-question-msg">
                  {{item2.index}}
                  {{item2.Phenomenon}}
                  {{item2.similarity}}
                </div>
              </div>
            </div>
            <!-- reasons & methods -->
            <div v-for="(item2,index) in item.reason" :key="index">
              <div class="con_que" @click="sentMsgByVal2(item2.id, item2.content.id)">
                <div class="czkj-question-msg">
                  {{item2.index}}&nbsp&nbsp&nbsp
                  原因：{{item2.content.reason}}&nbsp&nbsp&nbsp
                  方法：{{item2.content.method}}
                </div>
              </div>
            </div>

            <!-- details -->
            <!-- <div v-for="(item2,index) in item.details" :key="index">
              <div class="con_base_que">
                <div class="czkj-reason-msg">
                  {{ item2.pid }}
                  {{ item2.phenomenon }}
                  {{ item2.location }}
                  {{ item2.nid }}
                  {{ item2.reason }}
                  {{ item2.method }}
                  {{ item2.material }}
                  {{ item2.zid }}
                  {{ item2.zongcheng }}
                </div>
              </div>
            </div> -->
            <el-dialog title="详情信息" :visible.sync="dialogTableVisible">
              <el-table :data="gridData">
                <el-table-column property="key" label="属性" width="300"></el-table-column>
                <el-table-column property="val" label="内容" width="500"></el-table-column>
              </el-table>
            </el-dialog>

          </div>
          <!-- <div class="time_r">{{item.time}}</div> -->
        </div>
        <!-- user -->
        <div class="info_l" v-if="item.type == 'rightinfo'">
          <div class="time_l">{{item.time}}</div>
          <!-- <div class="con_r con_text"> -->
            <span class="con_l">{{item.content}}</span>
            <span class="circle circle_l">
              <!-- <img src class="pic_l" /> -->
              <!-- <i class="el-icon-user"></i> -->
            </span>
          <!-- </div> -->
          <!-- <div class="time_l">{{item.time}}</div> -->
        </div>
      </div>
    </div>
    <div class="setproblem">
      <textarea
        placeholder="请输入您的问题..."
        id="text"
        v-model="customerText"
        @keyup.enter="sentMsg()"
      ></textarea>
      <el-button @click="sentMsg()" class="buttonsend">
        <span style="vertical-align: 4px;">发送</span>
      </el-button>
    </div>
  </div>
</template>



<script>
import {request} from "../../network/2";

export default {
      data() {
        return {
          customerText: "",
          info: [
            {
              type: 'leftinfo',
              time: this.getTodayTime(),
              name: "robot",
              content:"您好，您可能想问!",
              question: [
                { id: 0, Phenomenon: "辊磨机轴承温度过高", index: 1 },
                { id: 1, Phenomenon: "辊磨机辊缝过小", index: 2 },
                { id: 2, Phenomenon: "减速机温度高", index: 3 },
                { id: 3,Phenomenon: "减速机振动大，声音异常",index: 4 },
                { id: 4, Phenomenon: "辊磨机辊子不转", index: 5 }
              ],
              reason: [],  // {'id': 1, 'Phenomenon': '辊磨机辊缝过小', 'index': 1, 'content': {'id': 31, 'reason': '辊面磨损', 'method': '检查辊面是否磨损，磨损将影响两辊间物料成型，严重引起扭力盘和减速机振动'}}
              // details: []
            }
          ],
          timer: null,
          dialogTableVisible: false,
          gridData: [],
        }
      },
      created() {
        this.showTimer();
      },
      methods: {
        // 用户发送消息
        sentMsg() {
          clearTimeout(this.timer)
          this.showTimer()
          let text = this.customerText.trim()
          if (text != '') {
            var obj = {
              type: 'rightinfo',
              time: this.getTodayTime(),
              content: text,
            }
            this.info.push(obj)
            this.appendRobotMsg(this.customerText)
            this.customerText = ''
            this.$nextTick(() => {
              var contentHeight = document.getElementById('content')
              contentHeight.scrollTop = contentHeight.scrollHeight
            })
          }
        },
        // 机器人回答消息
        async appendRobotMsg(text) {
          clearTimeout(this.timer)
          this.showTimer()
          text = text.trim()
          let answerText = ''

          let answer = await this.fuzzyQuery(text)
          console.log(answer)

          let msg = ""
          // console.log(answer.data)
          if (null != answer.data && "" != answer.data) {
            msg = "您可能想问："
          }else{
            msg = "抱歉，未匹配到相应内容！"
          }

          let obj = {
            type: 'leftinfo',
            time: this.getTodayTime(),
            name: "robot",
            content: msg,
            question: answer.data,
            reason: [],
            // details: []
          }
          this.info.push(obj)

          this.$nextTick(() => {
            var contentHeight = document.getElementById('content')
            contentHeight.scrollTop = contentHeight.scrollHeight
          })
        },
        async sentMsgByVal(val) {
          clearTimeout(this.timer)
          this.showTimer()
          // let robotById = this.robotAnswer.filter((item) => {
          //   return item.id == id;
          // })
          let answer = await this.exactQuery(val)
          console.log(answer)
          let obj_l = {
            type: 'leftinfo',
            time: this.getTodayTime(),
            name: 'robot',
            // content: answer.data[0].content,
            content: '可能的原因及方法',
            question: [],
            reason: answer.data,
            // details: []
          };
          let obj_r = {
            type: 'rightinfo',
            time: this.getTodayTime(),
            name: 'robot',
            content: answer.data[0].Phenomenon,
            question: [],
            reason: [],
            // details: []
          };
          this.info.push(obj_r)
          this.info.push(obj_l)
          this.$nextTick(() => {
            var contentHeight = document.getElementById('content')
            contentHeight.scrollTop = contentHeight.scrollHeight
          })
        },
        async sentMsgByVal2(val1, val2){
          clearTimeout(this.timer)
          this.showTimer()

          let answer = await this.exactQuery2(val1, val2)
          console.log(answer)
          this.gridData = answer.data
          this.dialogTableVisible = true
          // let obj_l = {
          //   type: 'leftinfo',
          //   time: this.getTodayTime(),
          //   name: 'robot',
          //   // content: answer.data[0].content,
          //   content: 'details',
          //   question: [],
          //   reason: [],
          //   details: answer.data,
          // };
          // let obj_r = {
          //   type: 'rightinfo',
          //   time: this.getTodayTime(),
          //   name: 'robot',
          //   content: '详情',
          //   question: [],
          //   reason: [],
          //   details: []
          // };
          // this.info.push(obj_r)
          // this.info.push(obj_l)          
          // this.$nextTick(() => {
          //   var contentHeight = document.getElementById('content')
          //   contentHeight.scrollTop = contentHeight.scrollHeight
          // })
        },
        // 结束语
        endMsg() {
          let happyEnding = {
            type: 'leftinfo',
            time: this.getTodayTime(),
            content: "退出",
            question: [],
            reason: [],
            // details: []
          };
          this.info.push(happyEnding)
          this.$nextTick(() => {
            var contentHeight = document.getElementById('content')
            contentHeight.scrollTop = contentHeight.scrollHeight
          })
        },
        showTimer() {
          this.timer = setTimeout(this.endMsg, 1000*60)
          // console.log(this.timer)
          // console.log(this.getTodayTime())
        },
        getTodayTime() {
          // 获取当前时间
          var day = new Date()
          let seconds = day.getSeconds()
          if (seconds < 10) {
            seconds = "0" + seconds
          } else {
            seconds = seconds
          }
          let minutes = day.getMinutes()
          if (minutes < 10) {
            minutes = "0" + minutes
          } else {
            minutes = minutes
          }
          let time =
            day.getFullYear() +
            "-" +
            (day.getMonth() + 1) +
            "-" +
            day.getDate() +
            " " +
            day.getHours() +
            ":" +
            minutes +
            ":" +
            seconds
          return time
        },
        async exactQuery(val){
          let res = await request('/esearch', {id: val})
          console.log('exactQuery OK')
          return res
        },
        async exactQuery2(val1, val2){
          let res = await request('/details',{pid: val1, nid: val2})
          console.log('details OK')
          return res
        },
        async fuzzyQuery(val){
          val = val.trim()
          let res =await request('/fsearch_unique', {msg: val})
          console.log('fuzzyQuery OK')
          return res
        }
      }
    }

</script>
<style scoped>
      .box {
        /* width: 100%; */
        height: 500px;
        background-color: #fafafa;
        position: relative;
        padding: 20px;
      }
        #content {
          /* height: 340px; */
          height: 720px;
          overflow-y: scroll;
          font-size: 14px;
          width: 100%;
        }
          .circle {
            display: inline-block;
            width: 34px;
            height: 34px;
            border-radius: 50%;
            background-color: #105fad;
          }
          .con_text {
            color: #333;
            margin-bottom: 5px;
          }
          .con_base_que{
            height: 30px;
            line-height: 30px;
          }
          .con_que {
            color: #1c88ff;
            height: 30px;
            line-height: 30px;
            cursor: pointer;
          }
          .info_r {
            position: relative;
          }
            .circle_r {
              position: absolute;
              left: 0%;
            }
            .pic_r {
              width: 17px;
              height: 17px;
              margin: 8px;
            }
            .con_r {
              display: inline-block;
              width: 55%;
              min-height: 55px;
              background-color: #e2e2e2;
              border-radius: 6px;
              padding: 10px;
              margin-left: 40px;
            }
            .time_r {
              margin-left: 45px;
              color: #999999;
              font-size: 12px;
            }
          .info_l {
            text-align: right;
            color: #ffffff;
            color: #3163C5;
            margin-top: 10px;
          }
            .pic_l {
              width: 13px;
              height: 17px;
              margin: 8px 10px;
            }
            .time_l {
              margin-right: 45px;
              color: #999999;
              font-size: 12px;
              margin-top: 5px;
            }
            .con_l {
              display: inline-block;
              width: 220px;
              min-height: 51px;
              background-color: #3163C5;
              border-radius: 6px;
              padding: 10px;
              text-align: left;
              color: #fff;
              margin-right: 5px;
            }
          #question {
            cursor: pointer;
          }
    .setproblem {
      width: 90%;
      height: 68px;
      background-color: #ffffff;
      /* background-color: #1c88ff; */
      position: relative;
      /* position: fixed; */
      margin-bottom: 20px;
      margin-top: 20px;
      padding-bottom: 20px; 
      /* bottom: 20%; */
      /* border: solid #1c88ff; */
    }
    .setproblem textarea {
      margin-bottom: 10px;
      color: #999999;
      padding: 10px;
      box-sizing: border-box;
      height: 68px;
      width: 100%;
      resize:none;
      padding-right:80px;
      outline: none;
      border-color:#ccc;
      border-radius:5px;
    }
    .buttonsend {
      background: #3163C5;
      opacity: 1;
      border-radius: 4px;
      font-size: 10px;
      color: #ffffff;
      position: absolute;
      right: -10%;
      top: 30%;
      cursor: pointer;
      /* border: solid #1c88ff; */
    }
   
    .czkj-item-title {
      line-height: 25px;
      border-bottom: 1px solid #ccc;
      padding-bottom: 5px;
      margin-bottom: 5px;
    }
   
    .czkj-item-question {
      cursor: pointer;
      display: block;
      padding: 8px;
      position: relative;
      border-bottom: 1px dashed #ccc;
      line-height: 20px;
      min-height: 20px;
      overflow: hidden;
    }
   
    .czkj-question-msg {
      float: left;
      font-size: 14px;
      color: #3163C5;
    }
    .czkj-reason-msg{
      color: #000;
      float: left; 
      font-size: 14px;
    }
  </style>
